package com.xgp.OJ;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class Demo1 {
    public static void main(String[] args) {
        // Java调用 dos命令
        String cmd1 = "gcc D:\\develop\\java\\IDEA\\111\\juc\\src\\main\\java\\com\\xgp\\OJ\\main.cpp -o D:\\develop\\java\\IDEA\\111\\juc\\src\\main\\java\\com\\xgp\\OJ\\main";
        String cmd2 = "D:\\develop\\java\\IDEA\\111\\juc\\src\\main\\java\\com\\xgp\\OJ\\main.exe";
        System.out.println(Arrays.asList(compileCppAndJava(cmd1,cmd2)));

        String cmd3 = "python D:\\develop\\java\\IDEA\\111\\juc\\src\\main\\java\\com\\xgp\\OJ\\main.py";
        System.out.println(Arrays.asList(compilePyAndJs(cmd3)));

        String cmd4 = "jjs D:\\develop\\java\\IDEA\\111\\juc\\src\\main\\java\\com\\xgp\\OJ\\main.py";
        System.out.println(Arrays.asList(compilePyAndJs(cmd4)));

        String cmd5 = "javac D:\\develop\\java\\IDEA\\111\\juc\\src\\main\\java\\com\\xgp\\OJ\\main.java";
        String cmd6 = "cmd /k \"cd /d D:\\develop\\java\\IDEA\\111\\juc\\src\\main\\java\\com\\xgp\\OJ\" && java main && exit";

        System.out.println(Arrays.asList(compileCppAndJava(cmd5,cmd6)));
    }

    private static String[] compilePyAndJs(String cmd) {
        String[] res = startCmd(cmd);
        if(res[0].isEmpty()) {
            res[0] = "语法错误";
            res[1] = null;
            res[2] = null;
        }
        return res;
    }

    private static String[] compileCppAndJava(String cmd1,String cmd2) {
        String[] res = startCmd(cmd1);
        if(res != null || res[0].isEmpty()) {
            //C语言编译成功了
            res = startCmd(cmd2);
            if(res[0].isEmpty()) {
                res[0] = "语法错误";
                res[1] = null;
                res[2] = null;
            }
        }
        return res;
    }

    private static String[] startCmd(String cmd) {
        String str = "";
        String arr[] = new String[3];
        try {
            //统计时间和内存
            long startMemory = Runtime.getRuntime().freeMemory();
            long start = System.currentTimeMillis();
            Process process = Runtime.getRuntime().exec(cmd);
            long end = System.currentTimeMillis();
            long endMemory = Runtime.getRuntime().freeMemory();

            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String content = br.readLine();
            while (content != null) {
                str = str + content + "\n";
                content = br.readLine();
            }
            arr[0] = str;
            arr[1] = String.valueOf(end - start);
            arr[2] = String.valueOf(startMemory - endMemory);
        } catch (IOException e) {
            if(e.getMessage().contains("系统找不到指定的文件")) {
                arr[0] = "语法错误";
                arr[1] = null;
                arr[2] = null;
            }else {
                e.printStackTrace();
            }
        }
        return arr;
    }
}
